package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.*;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.HttpUtil;
import com.sailfish.springbootdemo.utils.ResultUtil;
import com.sailfish.springbootdemo.utils.redis.RedisCRUD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ThirdApiSendServiceImpl implements ThirdApiSendService {

    @Value("${personal.config.apis.sign}")
    private String sign;

    @Value("${personal.config.apis.get-all-capacity-request-key-api}")
    private String getAllCapacityRequestKeyApi;

    @Autowired
    private PcbaCompanyDao pcbaCompanyDao;

    @Autowired
    private PcbaLineDao pcbaLineDao;

    @Autowired
    private LineLevelDao lineLevelDao;

    @Autowired
    private PcbaWorkshopDao pcbaWorkshopDao;

    @Autowired
    private PcbaProcessDao pcbaProcessDao;

    @Autowired
    private PcbaDeviceDao pcbaDeviceDao;

    @Autowired
    private PcbaDeviceParamDao pcbaDeviceParamDao;

    @Autowired
    private PcbaDeviceTypeDao pcbaDeviceTypeDao;

    @Autowired
    private LineTechCapacityValueDao lineTechCapacityValueDao;

    @Autowired
    private TechCapacityRuleDao techCapacityRuleDao;

    @Autowired
    private LineTechBigTypeDao lineTechBigTypeDao;

    @Autowired
    private CoreAlgorithmService coreAlgorithmService;

    @Autowired
    private LineTechCapacityValueService lineTechCapacityValueService;

    @Autowired
    private PcbaDeviceRelatedTechParamActualDao pcbaDeviceRelatedTechParamActualDao;

    @Autowired
    private PcbaDeviceRelatedDeviceParamDao pcbaDeviceRelatedDeviceParamDao;

    @Autowired
    private TechDeviceParamDao techDeviceParamDao;

    @Autowired
    private PcbaDeviceResourceDao pcbaDeviceResourceDao;

    @Autowired
    private LineBaseBigTypeDao lineBaseBigTypeDao;

    @Autowired
    private LineBaseSmallTypeDao lineBaseSmallTypeDao;

    @Autowired
    private RedisCRUD redisCRUD;

    public Result getAllCmAndLineCapacityAndDeviceCapacity(Integer companyId) {
        try {
            try {
                String jsonString = redisCRUD.getCmAndLineCapacityAndDeviceCapacityInfo(companyId.toString());
                if (jsonString != null) {
                    List<PcbaCompany> list = JSON.parseArray(jsonString, PcbaCompany.class);
                    return ResultUtil.success(list);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ResultUtil.success(getAllCmAndLineCapacityAndDeviceCapacityCommon(companyId));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public List<PcbaCompany> getAllCmAndLineCapacityAndDeviceCapacityCommon(Integer companyId) {
        List<PcbaCompany> allCompany = new ArrayList<>();
        if (companyId == 0 || "0".equals(companyId.toString())) {
            allCompany = pcbaCompanyDao.getAllCompany(null, null);
        } else {
            PcbaCompany pcbaCompanyById = pcbaCompanyDao.getPcbaCompanyById(companyId);
            if (pcbaCompanyById != null) {
                allCompany.add(pcbaCompanyById);
            }
        }

        for (PcbaCompany pcbaCompany : allCompany) {
            List<PcbaWorkshop> allPcbaWorkshopByCompanyId = pcbaWorkshopDao.getAllPcbaWorkshopByCompanyId(null, pcbaCompany.getPcbaCompanyId());
            for (PcbaWorkshop pcbaWorkshop : allPcbaWorkshopByCompanyId) {
                List<PcbaLine> allPcbaLine_all = pcbaLineDao.getAllOkLine(null, pcbaCompany.getPcbaCompanyId(), pcbaWorkshop.getPcbaWorkshopId(), null, 1, null);
                //List<PcbaLine> allPcbaLine = new ArrayList<>(); // 最终符合条件的线体
                List<String> processList = new ArrayList<>();

                // 过滤掉 线体中 基线资源和工艺资源及设备实际能力认证都正常才返回
                /*for (PcbaLine pcbaLine : allPcbaLine_all) {
                    List<PcbaDevice> pcbaDeviceList = pcbaDeviceDao.getPcbaDeviceByLine(pcbaLine.getPcbaLineId());
                    boolean flag = true;
                    for (PcbaDevice pcbaDevice : pcbaDeviceList) {
                        PcbaDeviceResource pcbaDeviceResource = pcbaDeviceResourceDao.getById(pcbaDevice.getPcbaDeviceResourceId());
                        if((pcbaDeviceResource.getAuditStatusBase() != null && pcbaDeviceResource.getAuditStatusBase() != 1) || (pcbaDeviceResource.getAuditStatusTech() != null && pcbaDeviceResource.getAuditStatusTech() != 1) || (pcbaDevice.getParamsStatus() != null && pcbaDevice.getParamsStatus() != 1)){
                            flag = false;
                            break;
                        }
                    }
                    if(flag){
                        allPcbaLine.add(pcbaLine);
                    }
                }*/

                for (PcbaLine pcbaLine : allPcbaLine_all) {
                    pcbaLine.setLineLevel(lineLevelDao.getLineLevelById(pcbaLine.getPcbaLineLevel()));
                    //pcbaLine.setPcbaWorkshop(pcbaWorkshopDao.getPcbaWorkshopById(pcbaLine.getPcbaWorkshopId()));
                    PcbaProcess pcbaProcessById = pcbaProcessDao.getPcbaProcessById(pcbaLine.getPcbaProcessId());
                    if (pcbaProcessById != null) {
                        pcbaLine.setPcbaProcessName(pcbaProcessById.getPcbaProcessNameCn());
                        if (!processList.contains(pcbaProcessById.getPcbaProcessNameCn())) {
                            processList.add(pcbaProcessById.getPcbaProcessNameCn());
                        }
                    }

                    List<PcbaDevice> pcbaDeviceByLine = pcbaDeviceDao.getPcbaDeviceByLine(pcbaLine.getPcbaLineId());
                    if (pcbaDeviceByLine != null) {
                        for (PcbaDevice pcbaDevice : pcbaDeviceByLine) {
                            PcbaDeviceType pcbaDeviceType = pcbaDeviceTypeDao.getById(pcbaDevice.getPcbaDeviceTypeId());
                            Integer pcbaDeviceResourceId = pcbaDevice.getPcbaDeviceResourceId();
                            if (pcbaDeviceType != null) {
                                pcbaDevice.setPcbaDeviceTypeName(pcbaDeviceType.getPcbaDeviceTypeNameCn());
                            }

                            List<PcbaDeviceRelatedDeviceParam> pcbaDeviceRelatedDeviceParamList = new ArrayList<>();
                            List<PcbaDeviceParam> allPcbaDeviceParam = pcbaDeviceParamDao.getAllPcbaDeviceParam(pcbaDevice.getPcbaDeviceTypeId(), null, null);
                            for (PcbaDeviceParam pcbaDeviceParam : allPcbaDeviceParam) {
                                String pcbaDeviceParamKey = pcbaDeviceParam.getPcbaDeviceParamKey();
                                String pcbaDeviceParamNameCn = pcbaDeviceParam.getPcbaDeviceParamNameCn();
                                PcbaDeviceRelatedDeviceParam p = new PcbaDeviceRelatedDeviceParam();
                                PcbaDeviceRelatedDeviceParam pcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId(pcbaDeviceParamKey, pcbaDevice.getPcbaDeviceResourceId());
                                if (pcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId != null) {
                                    p.setPcbaDeviceParamKey(pcbaDeviceParamKey);
                                    //p.setPcbaDeviceParamName(pcbaDeviceParamNameCn);
                                    p.setPcbaDeviceParamValue(CommonUtils.handleZhuanyi(pcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId.getPcbaDeviceParamValue()));
                                } else {
                                    p.setPcbaDeviceParamKey(pcbaDeviceParamKey);
                                    //p.setPcbaDeviceParamName(pcbaDeviceParamNameCn);
                                    p.setPcbaDeviceParamValue("");
                                }
                                pcbaDeviceRelatedDeviceParamList.add(p);
                            }
                            PcbaDeviceRelatedDeviceParam sp = new PcbaDeviceRelatedDeviceParam();
                            // input_1689737799300, select_1689738666668
                            PcbaDeviceRelatedDeviceParam input_1689737799300 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689737799300", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam select_1689738666668 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("select_1689738666668", pcbaDeviceResourceId);
                            sp.setPcbaDeviceParamValue((input_1689737799300 != null ? CommonUtils.handleZhuanyi(input_1689737799300.getPcbaDeviceParamValue()) : "") + "^^" + (select_1689738666668 != null ? CommonUtils.handleZhuanyi(select_1689738666668.getPcbaDeviceParamValue()) : ""));
                            sp.setPcbaDeviceParamKey("input_1689737799300^^select_1689738666668");
                            pcbaDeviceRelatedDeviceParamList.add(sp);

                            PcbaDeviceRelatedDeviceParam sp2 = new PcbaDeviceRelatedDeviceParam();
                            // input_1689738115870^^input_1689738129478
                            PcbaDeviceRelatedDeviceParam input_1689738115870 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689738115870", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam input_1689738129478 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689738129478", pcbaDeviceResourceId);
                            sp2.setPcbaDeviceParamValue((input_1689738115870 != null ? CommonUtils.handleZhuanyi(input_1689738115870.getPcbaDeviceParamValue()) : "") + "^^" + (input_1689738129478 != null ? CommonUtils.handleZhuanyi(input_1689738129478.getPcbaDeviceParamValue()) : ""));
                            sp2.setPcbaDeviceParamKey("input_1689738115870^^input_1689738129478");
                            pcbaDeviceRelatedDeviceParamList.add(sp2);

                            PcbaDeviceRelatedDeviceParam sp3 = new PcbaDeviceRelatedDeviceParam();
                            // input_1689755475595^^input_1689755489307
                            PcbaDeviceRelatedDeviceParam input_1689755475595 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689755475595", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam input_1689755489307 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689755489307", pcbaDeviceResourceId);
                            sp3.setPcbaDeviceParamValue((input_1689755475595 != null ? CommonUtils.handleZhuanyi(input_1689755475595.getPcbaDeviceParamValue()) : "") + "^^" + (input_1689755489307 != null ? CommonUtils.handleZhuanyi(input_1689755489307.getPcbaDeviceParamValue()) : ""));
                            sp3.setPcbaDeviceParamKey("input_1689755475595^^input_1689755489307");
                            pcbaDeviceRelatedDeviceParamList.add(sp3);

                            PcbaDeviceRelatedDeviceParam sp4 = new PcbaDeviceRelatedDeviceParam();
                            //  input_1689754813469^^ input_1689754832147
                            PcbaDeviceRelatedDeviceParam input_1689754813469 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689754813469", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam input_1689754832147 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689754832147", pcbaDeviceResourceId);
                            sp4.setPcbaDeviceParamValue((input_1689754813469 != null ? CommonUtils.handleZhuanyi(input_1689754813469.getPcbaDeviceParamValue()) : "") + "^^" + (input_1689754832147 != null ? CommonUtils.handleZhuanyi(input_1689754832147.getPcbaDeviceParamValue()) : ""));
                            sp4.setPcbaDeviceParamKey("input_1689754813469^^input_1689754832147");
                            pcbaDeviceRelatedDeviceParamList.add(sp4);

                            PcbaDeviceRelatedDeviceParam sp5 = new PcbaDeviceRelatedDeviceParam();
                            //  int_1691049846622^^ int_1691049943255
                            PcbaDeviceRelatedDeviceParam int_1691049846622 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("int_1691049846622", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam int_1691049943255 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("int_1691049943255", pcbaDeviceResourceId);
                            sp5.setPcbaDeviceParamValue((int_1691049846622 != null ? CommonUtils.handleZhuanyi(int_1691049846622.getPcbaDeviceParamValue()) : "") + "^^" + (int_1691049943255 != null ? CommonUtils.handleZhuanyi(int_1691049943255.getPcbaDeviceParamValue()) : ""));
                            sp5.setPcbaDeviceParamKey("int_1691049846622^^int_1691049943255");
                            pcbaDeviceRelatedDeviceParamList.add(sp5);

                            PcbaDeviceRelatedDeviceParam sp6 = new PcbaDeviceRelatedDeviceParam();
                            //   input_1689756968016^^input_1689756983703
                            PcbaDeviceRelatedDeviceParam input_1689756968016 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689756968016", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam input_1689756983703 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689756983703", pcbaDeviceResourceId);
                            sp6.setPcbaDeviceParamValue((input_1689756968016 != null ? CommonUtils.handleZhuanyi(input_1689756968016.getPcbaDeviceParamValue()) : "") + "^^" + (input_1689756983703 != null ? CommonUtils.handleZhuanyi(input_1689756983703.getPcbaDeviceParamValue()) : ""));
                            sp6.setPcbaDeviceParamKey("input_1689756968016^^input_1689756983703");
                            pcbaDeviceRelatedDeviceParamList.add(sp6);

                            PcbaDeviceRelatedDeviceParam sp7 = new PcbaDeviceRelatedDeviceParam();
                            //    input_1689752806809^^input_1689752866142
                            PcbaDeviceRelatedDeviceParam input_1689752806809 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689752806809", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam input_1689752866142 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689752866142", pcbaDeviceResourceId);
                            sp7.setPcbaDeviceParamValue((input_1689752806809 != null ? CommonUtils.handleZhuanyi(input_1689752806809.getPcbaDeviceParamValue()) : "") + "^^" + (input_1689752866142 != null ? CommonUtils.handleZhuanyi(input_1689752866142.getPcbaDeviceParamValue()) : ""));
                            sp7.setPcbaDeviceParamKey("input_1689752806809^^input_1689752866142");
                            pcbaDeviceRelatedDeviceParamList.add(sp7);

                            PcbaDeviceRelatedDeviceParam sp8 = new PcbaDeviceRelatedDeviceParam();
                            //   int_1691054958086^^int_1691054965997
                            PcbaDeviceRelatedDeviceParam int_1691054958086 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("int_1691054958086", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam int_1691054965997 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("int_1691054965997", pcbaDeviceResourceId);
                            sp8.setPcbaDeviceParamValue((int_1691054958086 != null ? CommonUtils.handleZhuanyi(int_1691054958086.getPcbaDeviceParamValue()) : "") + "^^" + (int_1691054965997 != null ? CommonUtils.handleZhuanyi(int_1691054965997.getPcbaDeviceParamValue()) : ""));
                            sp8.setPcbaDeviceParamKey("int_1691054958086^^int_1691054965997");
                            pcbaDeviceRelatedDeviceParamList.add(sp8);

                            PcbaDeviceRelatedDeviceParam sp9 = new PcbaDeviceRelatedDeviceParam();
                            //  double_1691115079139^^ double_1691115101973
                            PcbaDeviceRelatedDeviceParam double_1691115079139 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("double_1691115079139", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam double_1691115101973 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("double_1691115101973", pcbaDeviceResourceId);
                            sp9.setPcbaDeviceParamValue((double_1691115079139 != null ? CommonUtils.handleZhuanyi(double_1691115079139.getPcbaDeviceParamValue()) : "") + "^^" + (double_1691115101973 != null ? CommonUtils.handleZhuanyi(double_1691115101973.getPcbaDeviceParamValue()) : ""));
                            sp9.setPcbaDeviceParamKey("double_1691115079139^^double_1691115101973");
                            pcbaDeviceRelatedDeviceParamList.add(sp9);

                            PcbaDeviceRelatedDeviceParam sp10 = new PcbaDeviceRelatedDeviceParam();
                            //  input_1689749567958 ^^ input_1689749580784
                            PcbaDeviceRelatedDeviceParam input_1689749567958 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689749567958", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam input_1689749580784 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689749580784", pcbaDeviceResourceId);
                            sp10.setPcbaDeviceParamValue((input_1689749567958 != null ? CommonUtils.handleZhuanyi(input_1689749567958.getPcbaDeviceParamValue()) : "") + "^^" + (input_1689749580784 != null ? CommonUtils.handleZhuanyi(input_1689749580784.getPcbaDeviceParamValue()) : ""));
                            sp10.setPcbaDeviceParamKey("input_1689749567958^^input_1689749580784");
                            pcbaDeviceRelatedDeviceParamList.add(sp10);

                            PcbaDeviceRelatedDeviceParam sp11 = new PcbaDeviceRelatedDeviceParam();
                            //  input_1689749710038^^input_1689749722708
                            PcbaDeviceRelatedDeviceParam input_1689749710038 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689749710038", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam input_1689749722708 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689749722708", pcbaDeviceResourceId);
                            sp11.setPcbaDeviceParamValue((input_1689749710038 != null ? CommonUtils.handleZhuanyi(input_1689749710038.getPcbaDeviceParamValue()) : "") + "^^" + (input_1689749722708 != null ? CommonUtils.handleZhuanyi(input_1689749722708.getPcbaDeviceParamValue()) : ""));
                            sp11.setPcbaDeviceParamKey("input_1689749710038^^input_1689749722708");
                            pcbaDeviceRelatedDeviceParamList.add(sp11);

                            PcbaDeviceRelatedDeviceParam sp12 = new PcbaDeviceRelatedDeviceParam();
                            //  input_1689752960815^^input_1689752976358
                            PcbaDeviceRelatedDeviceParam input_1689752960815 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689752960815", pcbaDeviceResourceId);
                            PcbaDeviceRelatedDeviceParam input_1689752976358 = pcbaDeviceRelatedDeviceParamDao.getPcbaDeviceRelatedDeviceParamByKeyAndDeviceResourceId("input_1689752976358", pcbaDeviceResourceId);
                            sp12.setPcbaDeviceParamValue((input_1689752960815 != null ? CommonUtils.handleZhuanyi(input_1689752960815.getPcbaDeviceParamValue()) : "") + "^^" + (input_1689752976358 != null ? CommonUtils.handleZhuanyi(input_1689752976358.getPcbaDeviceParamValue()) : ""));
                            sp12.setPcbaDeviceParamKey("input_1689752960815^^input_1689752976358");
                            pcbaDeviceRelatedDeviceParamList.add(sp12);

                                /*List<PcbaDeviceRelatedDeviceParam> pcbaDeviceRelatedDeviceParamList = pcbaDeviceRelatedDeviceParamDao.getAllByDeviceResourceId(pcbaDevice.getPcbaDeviceResourceId());
                                for (PcbaDeviceRelatedDeviceParam pcbaDeviceRelatedDeviceParam : pcbaDeviceRelatedDeviceParamList) {
                                    PcbaDeviceParam pcbaDeviceParamByKey = pcbaDeviceParamDao.getPcbaDeviceParamByKey(pcbaDeviceRelatedDeviceParam.getPcbaDeviceParamKey());
                                    if(pcbaDeviceParamByKey != null){
                                        pcbaDeviceRelatedDeviceParam.setPcbaDeviceParamName(pcbaDeviceParamByKey.getPcbaDeviceParamNameCn());
                                    }
                                }*/

                            List<PcbaDeviceRelatedTechParamActual> pcbaDeviceRelatedTechParamActualList = new ArrayList<>();
                            List<TechDeviceParam> allTechDeviceParam = techDeviceParamDao.getAllTechDeviceParam(pcbaDevice.getPcbaDeviceTypeId(), null, null);
                            for (TechDeviceParam techDeviceParam : allTechDeviceParam) {
                                String techDeviceParamKey = techDeviceParam.getTechDeviceParamKey();
                                String techDeviceParamNameCn = techDeviceParam.getTechDeviceParamNameCn();
                                PcbaDeviceRelatedTechParamActual pcbaDeviceRelatedTechParamActualByPcbaDeviceIdAndKey = pcbaDeviceRelatedTechParamActualDao.getPcbaDeviceRelatedTechParamActualByPcbaDeviceIdAndKey(pcbaDevice.getPcbaDeviceId(), techDeviceParamKey);
                                PcbaDeviceRelatedTechParamActual p = new PcbaDeviceRelatedTechParamActual();
                                if (pcbaDeviceRelatedTechParamActualByPcbaDeviceIdAndKey != null) {
                                    p.setTechDeviceParamKey(techDeviceParamKey);
                                    //p.setTechDeviceParamName(techDeviceParamNameCn);
                                    p.setTechDeviceParamValue(CommonUtils.handleZhuanyi(pcbaDeviceRelatedTechParamActualByPcbaDeviceIdAndKey.getTechDeviceParamValue()));
                                } else {
                                    p.setTechDeviceParamKey(techDeviceParamKey);
                                    //p.setTechDeviceParamName(techDeviceParamNameCn);
                                    p.setTechDeviceParamValue("");
                                }
                                pcbaDeviceRelatedTechParamActualList.add(p);
                            }


                                /*List<PcbaDeviceRelatedTechParamActual> pcbaDeviceRelatedTechParamActualList = pcbaDeviceRelatedTechParamActualDao.getAllByDeviceIdActual(pcbaDevice.getPcbaDeviceId());
                                for (PcbaDeviceRelatedTechParamActual pcbaDeviceRelatedTechParamActual : pcbaDeviceRelatedTechParamActualList) {
                                    TechDeviceParam techDeviceParamByKey = techDeviceParamDao.getTechDeviceParamByKey(pcbaDeviceRelatedTechParamActual.getTechDeviceParamKey());
                                    if(techDeviceParamByKey != null){
                                        pcbaDeviceRelatedTechParamActual.setTechDeviceParamName(techDeviceParamByKey.getTechDeviceParamNameCn());
                                    }
                                }*/
                            pcbaDevice.setPcbaDeviceRelatedDeviceParamList(pcbaDeviceRelatedDeviceParamList);
                            pcbaDevice.setPcbaDeviceRelatedTechParamActualList(pcbaDeviceRelatedTechParamActualList);
                        }
                        pcbaLine.setPcbaDeviceList(pcbaDeviceByLine);
                    }

                        /*Result result = coreAlgorithmService.calcLineBaseCapacity(pcbaLine.getPcbaLineId());
                        System.out.println(result.getData().toString());
                        if(result.getCode() == 200){
                            Object data = result.getData();
                            HashMap<String,Object> map = (HashMap<String,Object>)data;
                            if(map.get("lineBaseBigTypeList") != null || !"".equals(map.get("lineBaseBigTypeList"))){
                                List<LineBaseBigType> lineBaseBigTypeList = (List<LineBaseBigType>)map.get("lineBaseBigTypeList");

                                List<BaseCapacityRule> ll = new ArrayList<>();
                                for (LineBaseBigType lineBaseBigType : lineBaseBigTypeList) {
                                    List<LineBaseSmallType> lineBaseSmallTypeList = lineBaseBigType.getLineBaseSmallTypeList();
                                    for (LineBaseSmallType lineBaseSmallType : lineBaseSmallTypeList) {
                                        List<BaseCapacityRule> baseCapacityRuleList = lineBaseSmallType.getBaseCapacityRuleList();
                                        for (BaseCapacityRule baseCapacityRule : baseCapacityRuleList) {
                                            LineBaseBigType lineBaseBigTypeById = lineBaseBigTypeDao.getLineBaseBigTypeById(baseCapacityRule.getLineBaseBigTypeId());
                                            LineBaseSmallType lineBaseSmallTypeById = lineBaseSmallTypeDao.getLineBaseSmallTypeById(baseCapacityRule.getLineBaseSmallTypeId());
                                            if(lineBaseBigTypeById != null){
                                                baseCapacityRule.setLineBaseBigType(lineBaseBigTypeById);
                                            }
                                            if(lineBaseSmallTypeById != null){
                                                baseCapacityRule.setLineBaseSmallType(lineBaseSmallTypeById);
                                            }
                                        }
                                        ll.addAll(baseCapacityRuleList);
                                    }
                                }

                                pcbaLine.setBaseFinalKeyAndValueList(ll);
                            }
                        }else{
                            return ResultUtil.error(500, result.getMsgCn(), result.getMsgEn(), null);
                        }*/
                }
                List<PcbaLine> smtList = new ArrayList<>();
                List<PcbaLine> pthList = new ArrayList<>();
                List<PcbaLine> wpthList = new ArrayList<>();
                List<PcbaLine> swpthList = new ArrayList<>();
                List<PcbaLine> pressfitList = new ArrayList<>();
                List<PcbaLine> offlineList = new ArrayList<>();
                for (PcbaLine pcbaLine : allPcbaLine_all) {
                    // 线体类别 1：SMT    2：离线设备  3：PTH
                    if (pcbaLine.getPcbaLineType() == 1) {
                            /*Result result2 = lineTechCapacityValueService.getAllLineTechCapacityValueByLineId(pcbaLine.getPcbaLineId());
                            if(result2.getCode() == 200){
                                List<Object> list = new ArrayList<>();
                                if(result2.getData() != null){
                                    LineTechBigType lineTechBigType = (LineTechBigType)result2.getData();
                                    List<LineTechSmallType> lineTechSmallTypeList = lineTechBigType.getLineTechSmallTypeList();
                                    if(lineTechSmallTypeList != null && lineTechSmallTypeList.size() > 0){
                                        for (LineTechSmallType lineTechSmallType : lineTechSmallTypeList) {
                                            List<TechCapacityRule> techCapacityRuleList = lineTechSmallType.getTechCapacityRuleList();
                                            if(techCapacityRuleList != null && techCapacityRuleList.size() > 0){
                                                for (TechCapacityRule techCapacityRule : techCapacityRuleList) {
                                                    list.add(techCapacityRule);
                                                }
                                            }
                                        }
                                    }
                                }
                                pcbaLine.setTechFinalKeyAndValueList(list);
                            }else{
                                return ResultUtil.error(500, result2.getMsgCn(), result2.getMsgEn(), null);
                            }*/
                        smtList.add(pcbaLine);
                    } else if (pcbaLine.getPcbaLineType() == 2) {
                        offlineList.add(pcbaLine);
                    } else if (pcbaLine.getPcbaLineType() == 3) {
                            /*Result result2 = lineTechCapacityValueService.getAllLineTechCapacityValueByLineId(pcbaLine.getPcbaLineId());
                            if(result2.getCode() == 200){
                                List<Object> list = new ArrayList<>();
                                if(result2.getData() != null){
                                    LineTechBigType lineTechBigType = (LineTechBigType)result2.getData();
                                    List<LineTechSmallType> lineTechSmallTypeList = lineTechBigType.getLineTechSmallTypeList();
                                    if(lineTechSmallTypeList != null && lineTechSmallTypeList.size() > 0){
                                        for (LineTechSmallType lineTechSmallType : lineTechSmallTypeList) {
                                            List<TechCapacityRule> techCapacityRuleList = lineTechSmallType.getTechCapacityRuleList();
                                            if(techCapacityRuleList != null && techCapacityRuleList.size() > 0){
                                                for (TechCapacityRule techCapacityRule : techCapacityRuleList) {
                                                    list.add(techCapacityRule);
                                                }
                                            }
                                        }
                                    }
                                }
                                pcbaLine.setTechFinalKeyAndValueList(list);
                            }else{
                                return ResultUtil.error(500, result2.getMsgCn(), result2.getMsgEn(), null);
                            }*/
                        // 判断是 wh-pth 还是sw-pth 还是 PRESSFIT
                        System.out.println(pcbaLine.getPcbaProcessName());
                        if (pcbaLine.getPcbaProcessName() != null && !"".equals(pcbaLine.getPcbaProcessName())) {
                            if ("W-PTH".equals(pcbaLine.getPcbaProcessName().toUpperCase())) {
                                wpthList.add(pcbaLine);
                            }
                            if ("SW-PTH".equals(pcbaLine.getPcbaProcessName().toUpperCase())) {
                                swpthList.add(pcbaLine);
                            }
                            if ("PRESSFIT".equals(pcbaLine.getPcbaProcessName().toUpperCase())) {
                                pressfitList.add(pcbaLine);
                            }
                        }
                    }
                }
                for (PcbaLine pcbaLine : smtList) {
                    // 查询线体的工艺能力

                    // 线体级别
                    Integer lineTechBigTypeId = 1;
                    Integer pcbaLineLevel = pcbaLine.getPcbaLineLevel();
                    List<LineTechBigType> allLineTechBigType = lineTechBigTypeDao.getAllLineTechBigType(null);
                    for (LineTechBigType lineTechBigType : allLineTechBigType) {
                        if (lineTechBigType.getLineLevelIds().contains(pcbaLineLevel.toString())) {
                            lineTechBigTypeId = lineTechBigType.getLineTechBigTypeId();
                            break;
                        }
                    }
                    List<TechCapacityRule> allTechCapacityRule = techCapacityRuleDao.getAllTechCapacityRule(null, lineTechBigTypeId, null);
                    List<JSONObject> techLineCapacityList = new ArrayList<>();
                    for (TechCapacityRule techCapacityRule : allTechCapacityRule) {
                        String lineTechParamKey = techCapacityRule.getLineTechParamKey();
                        //String techCapacityRuleName = techCapacityRule.getTechCapacityRuleName();
                        LineTechCapacityValue lineTechCapacityValue = lineTechCapacityValueDao.getLineTechCapacityValueByKeyAndLineId(pcbaLine.getPcbaLineId(), lineTechParamKey);
                        if (lineTechCapacityValue != null) {
                            JSONObject jsonObject_ = new JSONObject();
                            //LineTechCapacityParamValueActual l = new LineTechCapacityParamValueActual();
                            jsonObject_.put("lineTechCapacityParamValueActual", CommonUtils.handleZhuanyi(lineTechCapacityValue.getLineTechCapacityParamValueActual()));
                            jsonObject_.put("lineTechParamKey", lineTechParamKey);
//                            jsonObject.put("techCapacityRuleName", techCapacityRuleName);
//                            l.setLineTechParamKey(lineTechParamKey);
//                            l.setLineTechCapacityParamValueActual(CommonUtils.handleZhuanyi(lineTechCapacityValue.getLineTechCapacityParamValueActual()));
                            techLineCapacityList.add(jsonObject_);

                            pcbaLine.setTechCapacityValueList(techLineCapacityList);
                        }
                    }
                }
                pcbaWorkshop.setSmtPcbaLineList(smtList);
                pcbaWorkshop.setOfflinePcbaLineList(offlineList);
                pcbaWorkshop.setWpthPcbaLineList(wpthList);
                pcbaWorkshop.setSwpthPcbaLineList(swpthList);
                pcbaWorkshop.setPthPcbaLineList(pthList);
                pcbaWorkshop.setPressfitPcbaLineList(pressfitList);
                //pcbaWorkshop.setTechLineCapacityList(techLineCapacityList);
                pcbaWorkshop.setProcessList(processList);
            }
            pcbaCompany.setPcbaWorkshopList(allPcbaWorkshopByCompanyId);
        }
        //System.out.println(allCompany);
        return allCompany;
    }

    class LineTechCapacityParamValueActual{
        private String lineTechCapacityParamValueActual;
        private String lineTechParamKey;

        public String getLineTechCapacityParamValueActual() {
            return lineTechCapacityParamValueActual;
        }

        public void setLineTechCapacityParamValueActual(String lineTechCapacityParamValueActual) {
            this.lineTechCapacityParamValueActual = lineTechCapacityParamValueActual;
        }

        public String getLineTechParamKey() {
            return lineTechParamKey;
        }

        public void setLineTechParamKey(String lineTechParamKey) {
            this.lineTechParamKey = lineTechParamKey;
        }
    }

    @Override
    public JSONArray getAllPcbaRequestKey() {
        try {
            JSONObject params = new JSONObject();
            String md5Sign = CommonUtils.encryptToMD5(CommonUtils.encryptToMD5(sign));
            params.put("sign", md5Sign);
            System.out.println(CommonUtils.formatCurrentTime() + "==========================================");
            System.out.println("getAllCapacityRequestKeyApi 入参：" + params);
            String keyRes = HttpUtil.sendPostJson(getAllCapacityRequestKeyApi, params);
            System.out.println(CommonUtils.formatCurrentTime() + "==========================================");
            System.out.println("getAllCapacityRequestKeyApi 返回：" + keyRes);
            JSONObject resObj = JSONObject.parseObject(keyRes);
            if ("200".equals(resObj.getString("Code"))) {
                // 取 Data2 里面的集合
                JSONArray data2 = resObj.getJSONArray("Data2");
                JSONArray finalJa = new JSONArray();
                if (data2 != null && data2.size() > 0) {
                    for (int a = 0; a < data2.size(); a++) {
                        JSONObject jsonObject = data2.getJSONObject(a);
                        String valueKeys = jsonObject.getString("value");
                        if (valueKeys != null && !"".equals(valueKeys)) {
                            if (valueKeys.contains("\n")) {
                                valueKeys = valueKeys.replaceAll("\\n", "");
                            }
                            if (valueKeys.contains("\r")) {
                                valueKeys = valueKeys.replaceAll("\\r", "");
                            }
                            if (valueKeys.contains("^^")) {
                                String[] split = valueKeys.split("\\^\\^");
                                for (String s : split) {
                                    finalJa.add(s);
                                }
                            } else {
                                finalJa.add(valueKeys);
                            }
                        }
                    }
                }
                return finalJa;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void refreshPcbaDeviceParamIsRelatedPcba() {
        try {
            JSONArray allPcbaRequestKey = getAllPcbaRequestKey();
            List<PcbaDeviceParam> allPcbaDeviceParam = pcbaDeviceParamDao.getAllPcbaDeviceParam(null, null, null);
            for (PcbaDeviceParam pcbaDeviceParam : allPcbaDeviceParam) {
                Integer pcbaDeviceParamId = pcbaDeviceParam.getPcbaDeviceParamId();

                if (allPcbaRequestKey != null && allPcbaRequestKey.contains(pcbaDeviceParam.getPcbaDeviceParamKey())) {
                    pcbaDeviceParamDao.updateIsRelatedPcba(1, pcbaDeviceParamId);
                } else {
                    pcbaDeviceParamDao.updateIsRelatedPcba(2, pcbaDeviceParamId);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void refreshTechDeviceParamIsRelatedPcba() {
        try {
            JSONArray allPcbaRequestKey = getAllPcbaRequestKey();
            List<TechDeviceParam> allTechDeviceParam = techDeviceParamDao.getAllTechDeviceParam(null, null, null);
            for (TechDeviceParam techDeviceParam : allTechDeviceParam) {
                Integer techDeviceParamId = techDeviceParam.getTechDeviceParamId();

                if (allPcbaRequestKey.contains(techDeviceParam.getTechDeviceParamKey())) {
                    techDeviceParamDao.updateIsRelatedPcba(1, techDeviceParamId);
                } else {
                    techDeviceParamDao.updateIsRelatedPcba(2, techDeviceParamId);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void refreshTechCapacityRuleIsRelatedPcba() {
        try {
            JSONArray allPcbaRequestKey = getAllPcbaRequestKey();
            List<TechCapacityRule> allTechCapacityRule = techCapacityRuleDao.getAllTechCapacityRule(null, null, null);
            for (TechCapacityRule techCapacityRule : allTechCapacityRule) {
                Integer techCapacityRuleId = techCapacityRule.getTechCapacityRuleId();

                if (allPcbaRequestKey != null) {
                    if (allPcbaRequestKey.contains(techCapacityRule.getTechCapacityRuleId())) {
                        techCapacityRuleDao.updateIsRelatedPcba(1, techCapacityRuleId);
                    } else {
                        techCapacityRuleDao.updateIsRelatedPcba(2, techCapacityRuleId);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
